package com.sundae.smarthomedemo.Activitys;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.sundae.smarthomedemo.Adapter.HomeDeviceAdapter;
import com.sundae.smarthomedemo.Beans.DeviceBean;
import com.sundae.smarthomedemo.Beans.DeviceSQLBean;
import com.sundae.smarthomedemo.MyAppConfig;
import com.sundae.smarthomedemo.R;
import com.sundae.smarthomedemo.Utils.DaoControl;
import com.sundae.smarthomedemo.Utils.LogUtil;
import com.sundae.smarthomedemo.Utils.ToastUtils;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import immortalz.me.library.TransitionsHeleper;

public class MainActivity extends BaseActivity {


    private SharedPreferences sharedPreferences;
    private DaoControl daoControl;

    private ArrayList<DeviceBean> deviceBeanArrayList;
    private HomeDeviceAdapter homeDeviceAdapter;

    private CircleImageView iconButton;
    private TextView id_textView;
    private Button scan_btn;
    private ImageButton shop_button;

    private ListView listView;

    private MZBannerView mzBannerView;
    private ImageButton bannerImageView;

    private List<Integer> imgRes;
    private List<String> urlList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sharedPreferences = getSharedPreferences(MyAppConfig.ShareP_Name , 0);
        if(sharedPreferences.getBoolean("isFirst" , true))
//        if(true)
        {
            sharedPreferences.edit().putBoolean("isFirst" , false).commit();
            Intent intent = new Intent(MainActivity.this , WelcomeActivity.class);
            startActivity(intent);
        }

        daoControl = DaoControl.getInstance(getApplicationContext());

        StatusBarUtil.setColor(this , getResources().getColor(R.color.backgroundColor) , 100);

        initData();
        initView();

    }

    @Override
    protected void onStart() {
        super.onStart();
        if(mzBannerView != null) mzBannerView.start();
        MyAppConfig.user_name = sharedPreferences.getString("username" , "");
        if(!MyAppConfig.user_name.equals(""))
        {
            id_textView.setText("您好,戴季元 ID:" + MyAppConfig.user_name);
        }else{
            id_textView.setText("您好,请登陆");
        }

        initListView();
    }

    private void initView()
    {
        iconButton = (CircleImageView) findViewById(R.id.icon_button);
        id_textView = (TextView) findViewById(R.id.id_textView);
        listView = (ListView) findViewById(R.id.home_device_list);
        scan_btn = (Button) findViewById(R.id.scan_device_button);
        shop_button = (ImageButton) findViewById(R.id.shop_button);

        iconButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivity(new Intent(MainActivity.this , PersonalActivity.class));
                TransitionsHeleper.startActivity(MainActivity.this, PersonalActivity.class, iconButton);
            }
        });

        id_textView.setClickable(true);
        id_textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivity(new Intent(MainActivity.this , PersonalActivity.class));
                TransitionsHeleper.startActivity(MainActivity.this, PersonalActivity.class, iconButton);
            }
        });

        scan_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TransitionsHeleper.startActivity(MainActivity.this, SelectWiFiActivity.class, scan_btn);
            }
        });

        shop_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("url" , "http://shumei.ai");
                intent.setClass(MainActivity.this , ShopActivity.class);//打开一个activity
                startActivity(intent);

            }
        });

        mzBannerView = (MZBannerView) findViewById(R.id.mzbanner);
        mzBannerView.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int i) {
                LogUtil.e("banner click : " + i);
                Intent intent = new Intent();
                intent.setClass(MainActivity.this , ShopActivity.class);
                intent.putExtra("url" , urlList.get(i));
                startActivity(intent);
            }
        });

        mzBannerView.setPages(imgRes, new MZHolderCreator() {
            @Override
            public MZViewHolder createViewHolder() {
                return new MZViewHolder() {
                    @Override
                    public View createView(Context context) {
                        bannerImageView = new ImageButton(context);
                        bannerImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        return bannerImageView;
                    }

                    @Override
                    public void onBind(Context context, int i, Object o) {
                        bannerImageView.setImageResource((Integer) o);
                    }
                };
            }
        });
        mzBannerView.setDelayedTime(3000);
        mzBannerView.start();

    }

    private void initData(){
        imgRes = new ArrayList<>();
        imgRes.add(R.drawable.rsq_big);
        imgRes.add(R.drawable.gyg);
        imgRes.add(R.drawable.csq);
        urlList = new ArrayList<>();
        urlList.add("https://chaoshi.detail.tmall.com/item.htm?spm=a3204.7933263.0.0.5Q9nLt&id=540847631139&rewcatid=50514008");
        urlList.add("https://chaoshi.detail.tmall.com/item.htm?spm=a3204.7933263.0.0.LzL9ic&id=520851953957&rewcatid=50514008");
        urlList.add("https://s.taobao.com/search?spm=a230r.1.0.0.SCdxki&q=除湿机&spu_title=松京+DH02&app=detailproduct&pspuid=1144156&jc=1&cat=50040437&from_pos=19_50040437.bpvcombo_1_0_1144156");
    }

    private void initListView()
    {
        final ArrayList<DeviceSQLBean> deviceSQLBeanArrayList = daoControl.selectByUser(MyAppConfig.user_name);
        homeDeviceAdapter = new HomeDeviceAdapter(MainActivity.this ,  deviceSQLBeanArrayList);
        listView.setAdapter(homeDeviceAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //startActivity(new Intent(MainActivity.this , ControlActivity.class));
                Intent intent = new Intent(MainActivity.this , ControlActivity.class);
                selectDevice = deviceSQLBeanArrayList.get((int) id);
                TransitionsHeleper.startActivity(MainActivity.this, intent, view);
            }
        });
    }

    private long lastTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            long now = System.currentTimeMillis();
            if(now - lastTime > 2000)
            {
                ToastUtils.showShortToast(getApplicationContext() , "再按一次推出");
                lastTime = now;
                return true;
            }else{
                MyActivityManager.removeAllActivity();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPause() {
        super.onPause();
        mzBannerView.pause();//暂停轮播
    }

    @Override
    public void onResume() {
        super.onResume();
        mzBannerView.start();//开始轮播
    }

}
